<template>
  <!-- 时段用电 -->
  <div class="chart-box2">
    <div class="chart-title">时段用电</div>
    <div class="chart-content">
      <div style="height: 60%">
        <div class="chart-container" :id="chartId"></div>
      </div>
      <div style="height: 40%" class="legend">
        <div class="item">
          <div class="title green">工作时间能耗</div>
          <div class="number">
            <count-to
              :start-val="numTotal.workEnergy.oldVal"
              :end-val="numTotal.workEnergy.newVal"
              :duration="3000"
            />
            <span class="unit"> kw·h</span>
          </div>
        </div>
        <div class="item">
          <div class="title yellow">非工作时间能耗</div>
          <div class="number text-right">
            <count-to
              :start-val="numTotal.relaxEnergy.oldVal"
              :end-val="numTotal.relaxEnergy.newVal"
              :duration="3000"
            />
            <span class="unit"> kw·h </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  defineComponent,
  onMounted,
  reactive,
  toRefs,
  onBeforeUnmount,
  nextTick,
  watch,
  toRaw,
} from "vue";
import { Chart, registerTheme } from "@antv/g2";
import common from "../../utils/common";
import store from "../../../../store";
import CountTo from "../countTo/index.vue";

export default defineComponent({
  name: "TimeIntervalEl",
  components: { CountTo },
  setup() {
    const state = reactive({
      chartId: "timeIntervalElChart",
      chart: null,
      colors: ["#38b05c", "#f6bd16"],
      data: [],
      numTotal: {
        total: 0,
        workEnergy: { ...common.countToInit },
        relaxEnergy: { ...common.countToInit },
      },
    });

    const initChart = () => {
      const data = toRaw(state.data);

      state.chart = new Chart({
        container: state.chartId,
        autoFit: true,
        theme: "chartTheme",
        padding: [10, 30],
      });

      state.chart.data(data);

      state.chart.coordinate("theta", {
        startAngle: Math.PI, // 起始角度
        endAngle: Math.PI * 2, // 结束角度
        radius: 0.8,
        innerRadius: 0.75,
      });

      state.chart.tooltip(false);
      state.chart.legend(false);

      state.chart
        .interval()
        .adjust("stack")
        .position("value")
        .color("type", state.colors)
        .label("percent", (percent) => {
          return {
            content: (data) => {
              return `${percent}%`;
            },
            style: {
              fill: "rgba(255,255,255,0.8)",
              fontSize: 14,
            },
          };
        });
      state.chart
        .annotation()
        .text({
          position: ["50%", "50%"],
          content: "总能耗",
          style: {
            fontSize: 14,
            fill: common.theme.lightWhite,
            fontWeight: 500,
            textAlign: "center",
          },
          offsetY: 20,
        })
        .text({
          position: ["50%", "90%"],
          content: state.numTotal.total,
          style: {
            fontSize: 24,
            fill: "#fff",
            textAlign: "center",
            fontWeight: 600,
          },
        });

      state.chart.render();
    };

    watch(
      () => store.getters.centerControl.Energy_timePartEl,
      (val) => {
        if (val) {
          getData();
        }
      }
    );

    const getData = () => {
      const { Energy_timePartEl } = store.getters.centerControl;
      if (Energy_timePartEl) {
        state.data = Energy_timePartEl.list;
        if (state.data) {
          if (state.data.length > 1) {
            state.numTotal.workEnergy.oldVal = state.numTotal.workEnergy.newVal;
            state.numTotal.workEnergy.newVal = Number(state.data[0].value);
            state.numTotal.relaxEnergy.oldVal =
              state.numTotal.relaxEnergy.newVal;
            state.numTotal.relaxEnergy.newVal = Number(state.data[1].value);
            state.numTotal.total =
              Number(state.data[0].value) + Number(state.data[1].value);
          }
          if (state.chart) {
            state.chart.changeData(toRaw(state.data));
          } else {
            nextTick(() => {
              initChart();
            });
          }
        }
      }
    };

    onMounted(() => {
      registerTheme("chartTheme", common.chartTheme);
      getData();
    });

    onBeforeUnmount(() => {
      const chartBox = document.getElementById(state.chartId);
      if (chartBox) {
        chartBox.innerHTML = "";
      }
    });
    return {
      ...toRefs(state),
    };
  },
});
</script>

<style lang="less" scoped>
@import "../../styles/common.less";
.chart-content {
  display: flex;
  flex-direction: column;
  .legend {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 2rem;
    .item {
      .title {
        display: flex;
        align-items: center;
      }
      .green {
        &::before {
          content: "";
          display: block;
          margin-right: 0.5rem;
          width: 1.2rem;
          height: 1.2rem;
          background: @lightGreen;
        }
      }
      .yellow {
        margin-left: 1rem;
        text-align: right;
        &::before {
          content: "";
          display: block;
          margin-right: 0.5rem;
          width: 1.2rem;
          height: 1.2rem;
          background: @primaryYellow;
        }
      }
      .number {
        font-size: 2.8rem;
        .unit {
          font-size: 1.4rem;
          margin-left: 0.4rem;
        }
      }
    }
  }
}
</style>
